package com.tank;

import java.awt.*;
import java.util.Random;

public class Tank {

    private Dir dir = Dir.DOWN;

    private  static final int SPEED = 10;
    private  static final int SPEEDBAD = 6;
    private TankFrame tf;
    private  boolean living = true;
    private Random random = new Random();

    private  static  int WIDTH=ResourceMgr.goodtankU.getWidth();
    private  static  int HEIGHT = ResourceMgr.goodtankU.getHeight();

    private  boolean moving = true;

    public Group getGroup() {
        return group;
    }

    private Group group = Group.BAD;

    public Rectangle getRecttank() {
        return recttank;
    }

    private Rectangle recttank= new Rectangle(0, 0, WIDTH, HEIGHT);

    public static int getWIDTH() {
        return WIDTH;
    }

    public static int getHEIGHT() {
        return HEIGHT;
    }

    public Dir getDir() {
        return dir;
    }

    public void setDir(Dir dir) {
        this.dir = dir;
    }

    public boolean isMoving() {
        return moving;
    }

    public void setMoving(boolean moving) {
        this.moving = moving;
    }



    public Tank(int x, int y, Dir dir, Group group, TankFrame tf) {
        this.recttank.x = x;
        this.recttank.y = y;
        this.dir = dir;
        this.tf = tf;
        this.group = group;

        //自家坦克不要跑啦。
        if (group==Group.GOOD) moving=false;
    }

    public void Paint(Graphics g) {
        //Color c = g.getColor();
        //g.setColor(Color.yellow);
        //g.fillRect(x, y, 50, 50);
        //g.setColor(c);
        if (!living){
            tf.tanks.remove(this);
            //立马随机创建一辆新的出来
            tf.tanks.add(new Tank(random.nextInt(tf.getWidth()),
                                    random.nextInt(tf.getHeight()),
                    Dir.UP, Group.BAD, tf));
        }

        switch (dir){
            case LEFT:
                g.drawImage(this.group == Group.GOOD ? ResourceMgr.goodtankL : ResourceMgr.badtankL, recttank.x, recttank.y, null);
                break;
            case UP:
                g.drawImage(this.group == Group.GOOD ? ResourceMgr.goodtankU : ResourceMgr.badtankU, recttank.x, recttank.y, null);
                break;
            case RIGHT:
                g.drawImage(this.group == Group.GOOD ? ResourceMgr.goodtankR : ResourceMgr.badtankR, recttank.x, recttank.y, null);
                break;
            case DOWN:
                g.drawImage(this.group == Group.GOOD ? ResourceMgr.goodtankD : ResourceMgr.badtankD, recttank.x, recttank.y, null);;
                break;
        }


        move();
    }

    private void move() {
        if(!moving) return;

        int movespeed = SPEED;

        if (group == Group.BAD) movespeed = SPEEDBAD;

        switch (dir){
            case LEFT:
                recttank.x-=movespeed;
                break;
            case UP:
                recttank.y-=movespeed;
                break;
            case RIGHT:
                recttank.x+=movespeed;
                break;
            case DOWN:
                recttank.y+=movespeed;
                break;
        }

        /*
        if (recttank.x < 0) {
            recttank.x = 0;
        }
        else if (recttank.x > tf.getWidth() - this.WIDTH){
            recttank.x = tf.getWidth() - this.WIDTH;
        };

        if (recttank.y < 20) {
            recttank.y = 20;
        }
        else if (recttank.y > tf.getHeight() - this.HEIGHT){
            recttank.y = tf.getHeight() - this.HEIGHT;
        };
        */


        if (group == Group.BAD){
            //敌人随时开火
            if (random.nextInt(100) > 95) this.fire();
            //随机方向
            if (random.nextInt(100) > 95)
                randomdir();
        }

        boundscheck();
    }

    private void boundscheck() {
        if (this.recttank.x < 2) recttank.x = 2;
        if (this.recttank.y < 30) recttank.y = 30；

        if (this.recttank.x > TankFrame.WIDTH - Tank.WIDTH)
            recttank.x = TankFrame.WIDTH - Tank.WIDTH;
        if (this.recttank.y > TankFrame.HEIGHT - Tank.HEIGHT)
            recttank.y = TankFrame.HEIGHT - Tank.HEIGHT;
    }

    private void randomdir() {
        this.dir = Dir.values()[random.nextInt(4)];
    }

    public void fire() {
       int bx = this.recttank.x + Tank.WIDTH / 2 - Bullet.WIDTH /2;
       int by = this.recttank.y + Tank.HEIGHT / 2 - Bullet.HEIGHT /2;

        switch (dir){
            case LEFT:
                bx-=Tank.HEIGHT / 2;
                by+=3;
                break;
            case UP:
                by-=Tank.WIDTH / 2;
                bx+=1;
                break;
            case RIGHT:
                bx+=Tank.HEIGHT / 2;
                by+=3;
                break;
            case DOWN:
                by+=Tank.WIDTH / 2;
                bx-=1;
                break;
        }

        tf.bullets.add(new Bullet(bx, by, this.dir, this,  this.tf));
    }

    public void die() {
        int eX = recttank.x + WIDTH /2 - Explode.WIDTH /2;
        int eY = recttank.y + HEIGHT /2 - Explode.HEIGHT /2;
        tf.explodes.add(new Explode(eX, eY, tf));
        this.living = false;
    }
}
